/**
 * 防抖函数
 * @param func 要防抖的函数
 * @param wait 防抖等待时间（毫秒）
 * @param immediate 是否立即执行一次
 * @returns 返回一个防抖后的函数
 */
export function debounce<T extends (...args: any[]) => any>(
  func: T,
  wait: number,
  immediate: boolean = false
): (...args: Parameters<T>) => void {
  let timeout: number | undefined;

  return function (...args: Parameters<T>) {
    const later = () => {
      timeout = undefined;
      if (!immediate) func.apply(this, args);
    };

    const callNow = immediate && !timeout;

    if (timeout !== undefined) {
      clearTimeout(timeout);
    }

    timeout = window.setTimeout(later, wait);

    if (callNow) {
      func.apply(this, args);
    }
  };
}


export function is2DArray(arr: any): boolean {
  return Array.isArray(arr) && arr.length > 0 && Array.isArray(arr[0]);
}